package com.tth.learn.addproperties.java;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author tth
 * @Date 2022/12/26 10:04
 */
public class FileInOutputStreamTest {
    /**
     * 实现图片的复制
     */
    @Test
    public void test() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            // 1.File实例化，指明写出到的文件
            File srcFile = new File("image.png");
            File destFile = new File("image1.png");

            // 2.FileInputStream、FileOutputStream实例化，用于数据的读取与写出
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            // 3.复制
            byte[] buff = new byte[5];
            int len;
            while ((len = fis.read(buff)) != -1) {
                fos.write(buff, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.关闭流
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
